package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.Banner;
import com.xiaochong.tir.common.data.vo.BannerVo;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xiaochong.tir.manager.service.BannerService;
import org.springframework.web.servlet.ModelAndView;

import javax.ws.rs.GET;
import java.util.Date;

/**
 * @Author: WangXing
 * @Description:
 * @CreateDate: 2018/8/4 15:43
 */
@RestController
@RequestMapping(ManagerVersion.API_V1_PREFIX+"/banner/")
@Api(value = "AppBanner")
public class BannerController {

    @Autowired
    private BannerService BannerService;

    @Autowired
    private OSSTool ossTool;
    @Autowired
    private RedisServer redisServer;
    /**
     * Banner列表
     * @return
     */
    @GetMapping("banners")
    public Object bannerPage(){
        return new ModelAndView("banner/banerPage");
    }

    /**
     * 查询Banner列表
     * @param vo
     * @return
     */
    @ApiOperation(value = "查询AppBanner列表",notes = "查询AppBanner列表")
    @PostMapping("selBanner")
    public Object selBanner(BannerVo vo){
        PageInfo<Banner> info = BannerService.selectByBannerList(vo);
        for (int i = 0; i < info.getList().size(); i++) {
            Banner banner = info.getList().get(i);
            if(StringUtils.isNotBlank(banner.getBannerImg())){
                banner.setBannerImg(ossTool.getOSSUrl(banner.getBannerImg()));
            }
        }
        return new ReturnResult<Banner>(0,"成功",info.getList(),info.getTotal());
    }

    /**
     * 修改banner状态
     * @param banner
     * @return
     */
    @ApiOperation(value = "修改app banner状态",notes = "修改app banner状态")
    @PostMapping("enableBanner")
    public Object enableBanner(Banner banner){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改banner状态");
        Banner old = BannerService.selectByPrimaryKey(banner.getId());
        if(StringUtils.isNotBlank(String.valueOf(banner.getEnableFlag()))&&banner.getEnableFlag()){
            banner.setEnableFlag(false);
        }else {
            banner.setEnableFlag(true);
        }
        BannerService.updateByBannerKey(banner,old);
        return new ReturnResult<Banner>(0,"成功",1,1);
    }

    /**
     * 新增页面
     * @return
     */
    @GetMapping("addBanner")
    public Object addBanner(){
        return new ModelAndView("banner/addBannerPage");
    }

    /**
     * 新增banner
     * @param banner
     * @return
     */
    @ApiOperation(value = "新增app banner",notes = "新增app banner")
    @PostMapping("insBanner")
    public Object insBanner(Banner banner){
        redisServer.set(RedisKeysUtils.LOGINNAME,"新增banner");
        Banner old = BannerService.selectByEntity(banner);
        if(null != old){
            return new ReturnResult<Banner>(-1,"重复添加",1,1);
        }
        banner.setCreateTime(new Date());
        BannerService.insertSelective(banner);
        return new ReturnResult<Banner>(0,"成功",1,1);
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @ApiOperation(value = "查看app banner",notes = "查看app banner")
    @GetMapping("detilBanner")
    public Object detilBanner(@RequestParam("id")Integer id){
        Banner banner = BannerService.selectByPrimaryKey(id);
        ModelAndView mv = new ModelAndView("banner/viewBanner");
        if(StringUtils.isNotBlank(banner.getBannerImg())){
            banner.setBannerImg(ossTool.getOSSUrl(banner.getBannerImg()));
        }
        mv.addObject("banner",banner);
        return mv;
    }
}
